package com.weiyuwqj.server.service.contracts.impl;

import com.timevale.esign.sdk.tech.service.AccountService;
import com.timevale.esign.sdk.tech.service.EsignsdkService;
import com.timevale.esign.sdk.tech.service.SealService;
import com.timevale.esign.sdk.tech.service.factory.AccountServiceFactory;
import com.timevale.esign.sdk.tech.service.factory.EsignsdkServiceFactory;
import com.timevale.esign.sdk.tech.service.factory.SealServiceFactory;
import com.timevale.esign.sdk.tech.service.factory.SignServiceFactory;
import com.weiyuwqj.api.common.HtmlTranslator;
import com.weiyuwqj.api.model.BasUserInfo;
import com.weiyuwqj.api.model.BasUserSecurity;
import com.weiyuwqj.api.model.BusAssetInvest;
import com.weiyuwqj.api.model.SysContracts;
import com.weiyuwqj.api.service.BasUserInfoService;
import com.weiyuwqj.api.service.BasUserSecurityService;
import com.weiyuwqj.api.service.BusAssetInvestService;
import com.weiyuwqj.api.service.ContractsGeneratorService;
import com.weiyuwqj.api.tool.FreeMarkerTool;
import com.weiyuwqj.api.tool.QiniuUpload;
import com.weiyuwqj.api.util.DateUtils;
import com.weiyuwqj.server.service.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by huzj on 2016/10/10.
 */
@Service("contractsGeneratorService")
public class ContractsGeneratorServiceImpl extends BaseServiceImpl<SysContracts> implements ContractsGeneratorService {

    @Autowired
    private BusAssetInvestService assetsInvestService;

    @Autowired
    private BasUserInfoService userInfoService;

    @Autowired
    private BasUserSecurityService userSecurityService;

    private AccountService SERVICE = AccountServiceFactory.instance();

    private SealService SEAL = SealServiceFactory.instance();

    private com.timevale.esign.sdk.tech.service.SignService SignService = SignServiceFactory.instance();

    private static EsignsdkService SDK = EsignsdkServiceFactory.instance();


    /**
     * 合同生成
     *
     * @param newUserId   受让人用户ID
     * @param oldUserId   转让人用户ID
     * @param investId    资产ID
     * @param investOrder 订单号
     */
    @Override
    public void generateContracts(Integer newUserId, Integer oldUserId, String investId, String investOrder) {
        BasUserInfo cederInfo = userInfoService.getOneByUserId(oldUserId); // 转让人投资扩展信息
        BasUserSecurity cederSecurity = userSecurityService.getOneByUserId(oldUserId); // 转让人真实信息

        BasUserInfo assigneeInfo = userInfoService.getOneByUserId(newUserId); // 受让人扩展信息
        BasUserSecurity assigneeSecurity = userSecurityService.getOneByUserId(newUserId); // 受让人真实信息

        FreeMarkerTool generator = new FreeMarkerTool(); // freemarker初始化
        Map map = new HashMap(); // 属性
        map.put("investOrder", investOrder); // 订单ID
        map.put("year", DateUtils.getYear());
        map.put("month", DateUtils.getMonth());
        map.put("day", DateUtils.getDay());
        map.put("cederInfo", cederInfo); // 转让人投资扩展信息
        map.put("cederSecurity", cederSecurity); // 转让人真实信息
        map.put("assigneeInfo", assigneeInfo); // 受让人扩展信息
        map.put("assigneeSecurity", assigneeSecurity); // 受让人真实信息
        String fileName = String.valueOf(new Date().getTime());
        String sRootDir = generator.geneHtmlFile("transferAgreement.ftl", map, "html", fileName + ".html");
        String pdfPath = sRootDir + "/pdf/" + fileName + ".pdf";
        try {
            HtmlTranslator.html2pdf(sRootDir + "/html/" + fileName + ".html", pdfPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String cederAgreement = sRootDir + "/pdf/" + fileName + "cederAgreement.pdf";
        String assigneeDstFile = sRootDir + "/pdf/" + fileName + "assigneeAgreement.pdf";
        String url = QiniuUpload.uploadQiniu(pdfPath, fileName + ".pdf");// pdf上传

        SysContracts contracts = new SysContracts();
        contracts.setUserId(newUserId);
        contracts.setAddtime(new Date());
        contracts.setSerialNumber(investOrder);
        contracts.setUrl(url);
        contracts.setTitle("债权转让协议");
        contracts.setInvestId(oldUserId);
        save(contracts);
    }

    /**
     * 包债权转让合同生成
     *
     * @param newUserId     受让人用户ID
     * @param investId      资产ID
     * @param investOrder   订单号
     * @param packagePlanId 包计划ID
     */
    @Override
    public void generateContractsByPackage(Integer newUserId, Integer investId, String investOrder, Integer packagePlanId) {
        // 包债转合同
        BusAssetInvest oldestAssetInvest = assetsInvestService.getOldestAssetInvestByPackagePlanId(packagePlanId); // 转让人包投资信息
        this.generateContracts(newUserId, oldestAssetInvest.getUserId(), investId.toString(), investOrder); // 合同保存
    }

    /**
     * 标债权转让合同生成
     *
     * @param newUserId   受让人用户ID
     * @param oldUserId   转让人用户ID
     * @param investId    资产ID
     * @param investOrder 订单号
     */
    @Override
    public void generateContractsByItem(Integer newUserId, Integer oldUserId, String investId, String investOrder) {
        this.generateContracts(newUserId, oldUserId, investId, investOrder); // 合同保存
    }


}


